import HeadPrefab from "../../component/HeadPrefab";
import DateUtil from "../../engine/DateUtil";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { RoomModel } from "../model/RoomModel";
import { RoomPlayer } from "../model/RoomPlayer";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewApplyDismiss extends ViewBase {

    private defaultItemNode: cc.Node
    private btnAggree: cc.Node;
    private btnRefuse: cc.Node;



    public onInitChild(...optionalParams: any[]) {
        this.refresh()
    }

    onEnable(): void {
        super.onEnable()
        // this.setTopUI()
    }

    private refresh() {
        if (RoomModel.instance.isPlayVideo) {
            let canBgCloseNode: cc.Node = this.node.getChildByName("canBgCloseNode");
            if (canBgCloseNode) {
                canBgCloseNode.active = false
            }
        }
        this.btnAggree = this.findChild(this.node, "btnAggree")
        this.btnRefuse = this.findChild(this.node, "btnRefuse")
        this.btnAggree.active = true
        this.btnRefuse.active = true
        UIHelper.onAddEvent(this.btnAggree, () => {
            SocketClient.req_dismiss_answer(true)
        })
        UIHelper.onAddEvent(this.btnRefuse, () => {
            SocketClient.req_dismiss_answer(false)
        })
        this.defaultItemNode = this.findChild(this.node, "scrollView/view/content/defaultItemNode")
        this.hideAllChildren(this.defaultItemNode.parent)
        let txtApplicant: cc.Label = this.getChild(this.node, "txtApplicant", cc.Label)
        let applyPlayer: RoomPlayer = RoomPlayer.getPlayerByUserId(RoomModel.instance.game_info.apply_dismiss)
        if(applyPlayer){
            txtApplicant.string = applyPlayer.nick_name + "申请解散房间，是否同意?"
        }
        for (let i = 0; i < RoomModel.instance.playerList.length; i++) {
            let player: RoomPlayer = RoomModel.instance.playerList[i];
            if(player){
                let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
                if (!itemNode) {
                    itemNode = cc.instantiate(this.defaultItemNode)
                    itemNode.parent = this.defaultItemNode.parent
                }
                itemNode.active = true
                // let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
                // head.setHead(player.headimg, player.lv)
                let txtName: cc.Label = this.getChild(itemNode, "txtName", cc.Label);
                txtName.string = "玩家【"+player.nick_name+"】";
                let waitNode: cc.Node = this.findChild(itemNode, "waitNode")
                waitNode.active = false;
                let aggreeNode: cc.Node = this.findChild(itemNode, "aggreeNode")
                aggreeNode.active = false
                if (RoomModel.instance.game_info.apply_dismissList[i] == 0) {
                    waitNode.active = true
                }
                else if (RoomModel.instance.game_info.apply_dismissList[i] == 1) {
                    aggreeNode.active = true
                }
                if (player.userid == UserModel.instance.userId) {
                    if (aggreeNode.active) {
                        this.btnAggree.active = false
                        this.btnRefuse.active = false;
                    }
                }
            }
        }
    }

    protected update(dt: number): void {
        let txtLeftTime: cc.Label = this.getChild(this.node, "txtLeftTime", cc.Label);
        let cd: number = RoomModel.instance.conf.dismiss_time * 1000;
        let leftTime: number = Math.floor((RoomModel.instance.game_info.apply_dismiss_time + cd - DateUtil.now()) / 1000);
        if (leftTime < 0) {
            leftTime = 0
        }
        txtLeftTime.string = leftTime + "s"
    }
}
